package com.kamistoat.meimeimall.meimeimallproduct.service.impl;

import com.kamistoat.meimeimall.common.constant.DatabaseConstant;
import com.kamistoat.meimeimall.common.utils.PageUtils;
import com.kamistoat.meimeimall.common.utils.Query;
import com.kamistoat.meimeimall.common.constant.ProductConstant;
import com.kamistoat.meimeimall.meimeimallproduct.dao.AttrGroupDao;
import com.kamistoat.meimeimall.meimeimallproduct.dao.CategoryDao;
import com.kamistoat.meimeimall.meimeimallproduct.entity.AttrGroupEntity;
import com.kamistoat.meimeimall.meimeimallproduct.entity.CategoryEntity;
import com.kamistoat.meimeimall.meimeimallproduct.service.CategoryService;
import com.kamistoat.meimeimall.meimeimallproduct.vo.AttrGroupRelationVo;
import com.kamistoat.meimeimall.meimeimallproduct.vo.AttrResponseVo;
import com.kamistoat.meimeimall.meimeimallproduct.vo.AttrVo;
import com.kamistoat.meimeimall.meimeimallproduct.dao.AttrDao;
import com.kamistoat.meimeimall.meimeimallproduct.entity.AttrEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.kamistoat.meimeimall.meimeimallproduct.service.AttrService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    protected Logger logger = LoggerFactory.getLogger(AttrServiceImpl.class);

    @Autowired
    AttrDao attrDao;
    @Autowired
    AttrGroupDao attrGroupDao;
    @Autowired
    CategoryDao categoryDao;
    @Autowired
    CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 生成测试数据
     *
     * @param attrGroupNum 每个三级菜单的属性组数量
     * @param baseAttrNum  每组中基本属性个数
     * @param saleAttrNum  每组中销售属性个数
     */
    @Override
    public void constructTestAttrData(Integer attrGroupNum, Integer baseAttrNum, Integer saleAttrNum) {
        List<Long> allThirdCategoryIds = categoryDao.selectAllThirdCategoryId();
        logger.info("为" + allThirdCategoryIds.size() + "条三级分类生成" + allThirdCategoryIds.size() * attrGroupNum * (baseAttrNum + saleAttrNum) + "条属性");
        List<AttrGroupEntity> attrGroupEntities = new ArrayList<>();
        List<AttrEntity> attrEntities = new ArrayList<>();
        for (Long thirdCategoryId : allThirdCategoryIds) {
            for (int groupNum = 1; groupNum <= attrGroupNum; groupNum++) {
                AttrGroupEntity var1 = new AttrGroupEntity();
                var1.setAttrGroupName("分类" + thirdCategoryId + "测试属性分组" + groupNum);
                var1.setCatelogId(thirdCategoryId);
                var1.setDescript("分类" + thirdCategoryId + "测试属性分组" + groupNum + "详情");
                var1.setIcon("分类" + thirdCategoryId + "测试属性分组" + groupNum + "图标");
                var1.setCreateTime(new Date());
                var1.setUpdateTime(new Date());
                var1.setDelete(DatabaseConstant.UN_LOGISTIC_DELETED);
                attrGroupEntities.add(var1);
            }
        }
        attrGroupDao.insertBatch(attrGroupEntities);
        logger.info("生成" + attrGroupEntities.size() + "条属性分组");

        for (AttrGroupEntity group : attrGroupEntities) {
            for (int baseNum = 1; baseNum <= baseAttrNum; baseNum++) {
                AttrEntity var2 = new AttrEntity();
                var2.setAttrType(1);
                var2.setGroupId(group.getAttrGroupId());
                var2.setCatelogId(group.getCatelogId());
                var2.setAttrName(group.getAttrGroupName() + "测试基础属性" + baseNum);
                var2.setValueType(1);
                var2.setValueSelect("测试值1;测试值2;测试值3");
                var2.setCreateTime(new Date());
                var2.setUpdateTime(new Date());
                var2.setDelete(DatabaseConstant.UN_LOGISTIC_DELETED);
                attrEntities.add(var2);
            }
            for (int saleNum = 1; saleNum <= saleAttrNum; saleNum++) {
                AttrEntity var3 = new AttrEntity();
                var3.setAttrType(0);
                var3.setGroupId(group.getAttrGroupId());
                var3.setCatelogId(group.getCatelogId());
                var3.setAttrName(group.getAttrGroupName() + "测试销售属性" + saleNum);
                var3.setValueType(0);
                var3.setValueSelect("测试值1;测试值2;测试值3");
                var3.setCreateTime(new Date());
                var3.setUpdateTime(new Date());
                var3.setDelete(DatabaseConstant.UN_LOGISTIC_DELETED);
                attrEntities.add(var3);
            }
        }
        attrDao.insertBatch(attrEntities);
        logger.info("生成" + allThirdCategoryIds.size() * attrGroupNum * (baseAttrNum + saleAttrNum) + "条属性");
    }

}